<?php

class Jf_View_Helper_PaginationControl {

    public $view = null;

    protected static $_defaultViewPartial = null;

    public function setView (Jf_View_Interface $view) {
        $this -> view = $view;
        return $this;
    }

    public static function setDefaultViewPartial ($partial) {
        self::$_defaultViewPartial = $partial;
    }

    public static function getDefaultViewPartial () {
        return self::$_defaultViewPartial;
    }

    public function paginationControl (Zend_Paginator $paginator = null, $scrollingStyle = null, $partial = null, $params = null) {
        if ($paginator === null) {
            if (isset ($this -> view -> paginator) and $this -> view -> paginator !== null and $this -> view -> paginator instanceof Zend_Paginator) {
                $paginator = $this -> view -> paginator;
            } else {
                $e = new Jf_View_Exception ('No paginator instance provided or incorrect type');
                $e -> setView ($this -> view);
                throw $e;
            }
        }
        if ($partial === null) {
            if (self::$_defaultViewPartial === null) {
                $e = new Jf_View_Exception ('No view partial provided and no default set');
                $e -> setView ($this -> view);
                throw $e;
            }
            $partial = self::$_defaultViewPartial;
        }
        $pages = get_object_vars ($paginator -> getPages ($scrollingStyle));
        if ($params !== null) {
            $pages = array_merge ($pages, (array) $params);
        }
        return $this -> view -> render ($partial, $pages);
    }

}
